package cn.moningxi.controller;

import cn.moningxi.bean.SystemMenu;
import cn.moningxi.bean.SystemUser;
import cn.moningxi.bean.SystemUserRole;
import cn.moningxi.service.SystemMenuService;
import cn.moningxi.service.SystemUserRoleService;
import cn.moningxi.service.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/olalashop")
@SessionAttributes(value = "admin", types = String.class)
public class SystemUserController {

    @Autowired
    public SystemUserService systemUserService;

    @Autowired
    private SystemMenuService systemMenuService;

    @Autowired
    private SystemUserRoleService systemUserRoleService;

    /**
     * 跳转到后台管理员登陆界面
     *
     * @param modelAndView
     * @return
     */
    @RequestMapping("/sysAccountApi/login")
    public ModelAndView login(ModelAndView modelAndView) {
        modelAndView.setViewName("backstage/login");
        return modelAndView;
    }

    /**
     * 后台管理员进行登录
     * 登录失败则停留在登录界面，并提示账号或者密码输入错误
     * 登陆成功则进入系统管理界面，并将管理员的身份信息、拥有的菜单功能传递到管理界面
     *
     * @param modelAndView
     * @param systemUser
     * @return
     */
    @RequestMapping("/sysAccountApi/logining")
    public ModelAndView logining(ModelAndView modelAndView, SystemUser systemUser) {
        System.out.println(systemUser);
        SystemUser user = systemUserService.login(systemUser);
        System.out.println(user);
        if (user != null) {
            List<SystemMenu> menus = systemMenuService.queryMenuListByUserId(user.getUserId());
            modelAndView.addObject("menus", menus);
            modelAndView.addObject("currentLoginUser", user);
            modelAndView.addObject("admin", user);
            modelAndView.setViewName("backstage/index");
        } else {
            System.out.println("账号或者密码输入错误");
            modelAndView.addObject("loginAdmin", systemUser);
            modelAndView.addObject("message", "账号或者密码输入错误");
            modelAndView.setViewName("backstage/login");
        }
        return modelAndView;
    }

    /**
     * 返回首页
     *
     * @param modelAndView
     * @param request
     * @return
     */
    @RequestMapping("/sysAccountApi/index")
    public ModelAndView index(ModelAndView modelAndView, HttpServletRequest request) {
        SystemUser admin = (SystemUser) request.getSession().getAttribute("admin");
        modelAndView.addObject("currentLoginUser", admin);
        List<SystemMenu> menus = systemMenuService.queryMenuListByUserId(admin.getUserId());
        modelAndView.addObject("menus", menus);
        modelAndView.setViewName("backstage/index");
        return modelAndView;
    }

    /**
     * 查询出全部管理员信息
     *
     * @param modelAndView
     * @return
     */
    @RequestMapping("/sysAccountApi/allSystemUser")
    @ResponseBody
    public ModelAndView queryAllSystemUser(ModelAndView modelAndView) {
        List<SystemUser> systemUsers = systemUserService.queryAllSystemUser();
        modelAndView.addObject("users", systemUsers);
        modelAndView.setViewName("backstage/user-list");
        return modelAndView;
    }

    /**
     * 跳转到修改管理员信息界面
     *
     * @param modelAndView
     * @param userId
     * @return
     */
    @RequestMapping("/sysAccountApi/editSystemUser/{userId}")
    @ResponseBody
    public ModelAndView editSystemUser(ModelAndView modelAndView, @PathVariable("userId") Integer userId) {
        SystemUser editUser = systemUserService.querySystemUserById(userId);
        modelAndView.addObject("editUser", editUser);
        modelAndView.setViewName("backstage/editSystemUser");
        return modelAndView;
    }

    /**
     * 接收前端传来的修改后的用户信息，并修改
     *
     * @param systemUser
     * @param modelAndView
     * @return
     */
    @RequestMapping(value = "/sysAccountApi/editSystemUser/updateSystemUser", method = RequestMethod.POST)
    public ModelAndView updateSystemUser(ModelAndView modelAndView, SystemUser systemUser, HttpServletRequest httpServletRequest) {
        Integer rId = Integer.valueOf(httpServletRequest.getParameter("roleId"));
        Integer uId = systemUser.getUserId();
        System.out.println("测试systemUser：" + systemUser);
        System.out.println("测试rID：" + rId);
        SystemUserRole userRole = systemUserRoleService.queryByUserId(uId);
        if (userRole == null) {
            systemUserRoleService.insertUserRole(rId, uId);
        } else {
            systemUserRoleService.updateUserRole(rId, uId);
        }
        systemUserService.updateSystemUser(systemUser);
        modelAndView.setViewName("redirect:/olalashop/sysAccountApi/allSystemUser");
        return modelAndView;
    }

    /**
     * 根据userId删除用户信息
     *
     * @param modelAndView
     * @param userId
     * @return
     */
    @RequestMapping("/sysAccountApi/deleteSystemUser/{userId}")
    public ModelAndView deleteSystemUser(ModelAndView modelAndView, @PathVariable("userId") Integer userId) {
        systemUserService.deleteSystemUser(userId);
        modelAndView.setViewName("redirect:/olalashop/sysAccountApi/allSystemUser");
        return modelAndView;
    }

    /**
     * 添加管理员
     *
     * @param modelAndView
     * @param systemUser
     * @return
     */
    @RequestMapping(value = "/sysAccountApi/addSystemUser", method = RequestMethod.POST)
    public ModelAndView addSystemUser(ModelAndView modelAndView, SystemUser systemUser, HttpServletRequest httpServletRequest) {
        SystemUser userByAccount = systemUserService.queryUserByAccount(systemUser.getUserName());
        if (userByAccount != null) {
            modelAndView.addObject("msg", "账号已存在");
            modelAndView.setViewName("backstage/user-add");
        } else {
            systemUserService.addSystemUser(systemUser);
            modelAndView.setViewName("redirect:/olalashop/sysAccountApi/allSystemUser");
        }
        return modelAndView;
    }
}